﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using KC.Common;
using KC.Framework.Tenant;
using KC.Framework.Util;
using KC.Service.Core.WebApiService.Business;
using KC.DTO.Account;
using KC.Service.Core.DTO.Message;

namespace KC.Service.Core.Message
{
    public interface IMessageGenerator
    {
        string InputParameter { get; }
        Dictionary<string, string> ReplaceContent { get; }
        List<string> GetReplaceParameters();
        MessageClassDTO GenerateMessageClass();

        List<UserDTO> LoadUserDefaulAll(List<string> userIds = null);

    }

    public abstract class AbstractMessageGenerator : IMessageGenerator
    {
        protected Tenant Tenant { get; private set; }

        public string InputParameter { get; private set; }
        public string MessageCode { get; private set; }
        public Dictionary<string, string> ReplaceContent { get; set; }

        private IMessageApiService _messasgeApiService;

        protected AbstractMessageGenerator(
            Tenant tenant, string replaceObjId, string messagecode,
            IMessageApiService messageApiService,
            Microsoft.Extensions.Logging.ILogger logger)
            : this(tenant, replaceObjId, messagecode, null, messageApiService, logger)
        {
        }


        protected AbstractMessageGenerator(Tenant tenant, string replaceObjId, string messagecode,
            Dictionary<string, string> replaceDict,
            IMessageApiService messageApiService,
            Microsoft.Extensions.Logging.ILogger logger)
        {
            Tenant = tenant;
            InputParameter = replaceObjId;
            MessageCode = messagecode;
            ReplaceContent = replaceDict ?? new Dictionary<string, string>();
            _messasgeApiService = messageApiService;
        }

        public abstract List<string> GetReplaceParameters();
        public abstract MessageClassDTO GenerateMessageClass();

        protected MessageClassDTO GetMessageClassByCode()
        {
            var model = _messasgeApiService.GetMessageClassByCode(MessageCode);
            return model;
        }
        public void SetReplaceContent(object source)
        {
            var replaceParameter = GetReplaceParameters();
            foreach (var item in replaceParameter)
            {
                try
                {
                    var attribute = item.Substring(1, item.Length - 2);
                    Type type = source.GetType();
                    PropertyInfo field = type.GetProperty(attribute);
                    var attributeValue = field.GetValue(source);
                    var value = string.Empty;
                    if (attributeValue != null)
                    {
                        value = attributeValue.ToString();
                    }
                    if (!ReplaceContent.ContainsKey(item))
                    {
                        ReplaceContent.Add(item, value);
                    }
                    else
                    {
                        ReplaceContent[item] = value;
                    }
                }
                catch (Exception ex)
                {
                    ReplaceContent[item] = string.Empty;
                    LogUtil.LogDebug(string.Format("调用KC.Service.Core.Message.AbstractMessageGenerator中的SetReplaceContent方法体报错,参数如下：source={0}{1}错误消息：{2}", SerializeHelper.ToJson(source), Environment.NewLine, ex.Message));
                }

            }
        }
        protected string ReplaceContentByDict(string replaceContent)
        {
            //TODO: 使用模板引擎
            var result = replaceContent;
            if (ReplaceContent != null && ReplaceContent.Any())
            {
                result = ReplaceContent.Aggregate(replaceContent,
                    (current, keyValue) => current.Replace(keyValue.Key, keyValue.Value));
            }

            return result;
        }

        protected string GetFinancingOfferingUrl(string domanName, string offeringTenantName, int offeringId)
        {
            var offeringUrl = domanName.Replace(TenantConstant.SubDomain, offeringTenantName) +
                              "Financing/Details/" + offeringId;
            offeringUrl = string.Format("<a href='{0}' target='_blank'>{1}</a>", offeringUrl, "浏览");

            return offeringUrl;
        }

        protected string GetOfferingUrl(string domanName, string offeringTenantName, int offeringId)
        {
            var offeringUrl = domanName.Replace(TenantConstant.SubDomain, offeringTenantName) +
                              "Offering/Details/" + offeringId;
            offeringUrl = string.Format("<a href='{0}' target='_blank'>{1}</a>", offeringUrl, "浏览");

            return offeringUrl;
        }
        public List<UserDTO> LoadUserDefaulAll(List<string> userIds = null)
        {
            return _messasgeApiService.LoadUserDefaulAll(userIds);
        }


    }
}
